package com.mjh.zhujianju.controller.agency;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.livew.framework.base.controller.BaseController;
import com.livew.framework.base.model.FileEO;
import com.livew.framework.base.service.BaseService;
import com.livew.framework.base.service.FileService;
import com.livew.framework.exception.XTBusinessException;
import com.livew.framework.interceptor.URLPermission;
import com.livew.framework.pojo.AjaxResult;
import com.livew.framework.utils.XTEntityUtils;
import com.livew.framework.utils.XTListUtils;
import com.mjh.zhujianju.model.agency.OrganizationEO;
import com.mjh.zhujianju.service.agency.OrganizationService;
import com.mjh.zhujianju.vo.agency.OrganizationVO;

@Controller
@RequestMapping("agency/org")
public class OrgController extends BaseController<OrganizationEO> {

	@Resource
	private OrganizationService organizationService;
	
	@Resource
	private FileService fileService;

	@Override
	protected BaseService<OrganizationEO> getService() {

		return organizationService;
	}

	@RequestMapping(value = "index")
	@URLPermission
	public String index(Model model) {
		model.addAttribute("contents", null);
		return "agency/org/index";
	}

	//编辑
	@RequestMapping(value = "edit")
	public String edit(Model model) {
		OrganizationVO org = organizationService.getInfo();
		if(org!=null) {
			model.addAttribute("id", org.getId());
		}
		return "agency/org/edit";
	}
	
	/**
	 * 获取机构信息
	 * 
	 * @Description:
	 * @return
	 * @author wt1293
	 * @date 2018年8月9日下午8:20:32
	 *
	 */
	@RequestMapping(value = "getInfo", method = RequestMethod.GET)
	@ResponseBody
	public AjaxResult getInfo() {
		OrganizationVO org = organizationService.getInfo();
		if(org!=null) {
			List<FileEO> files= 	fileService.getFileById(org.getId(), OrganizationEO.class.getName(), null);
			if(!XTListUtils.isEmpty(files)) {
				org.setFiles(XTEntityUtils.getIdList(files));
			}
		}
		return AjaxResult.getResult(org);
	}


	/**
	 * 获取机构信息
	 * 
	 * @Description:
	 * @return
	 * @author wt1293
	 * @date 2018年8月9日下午8:20:32
	 *
	 */
	@RequestMapping(value = "saveInfo", method = RequestMethod.POST)
	@ResponseBody
	public AjaxResult saveInfo(OrganizationVO vo) {
		try {
			organizationService.saveInfo(vo);
		} catch (XTBusinessException e) {
			e.printStackTrace();
			return AjaxResult.getResult(Boolean.FALSE, e.getMessage());
		}
		return AjaxResult.getResult(Boolean.TRUE,"保存成功");
	}
	
	/**
	 * 
	 * 
	 * @Description:
	 * @return
	 * @author wt1293
	 * @date 2018年8月9日下午8:20:32
	 *
	 */
	@RequestMapping(value = "updateInfo", method = RequestMethod.POST)
	@ResponseBody
	public AjaxResult updateInfo(OrganizationVO vo) {
		try {
			organizationService.updateInfo(vo);
		} catch (XTBusinessException e) {
			e.printStackTrace();
			return AjaxResult.getResult(Boolean.FALSE, e.getMessage());
		}
		return AjaxResult.getResult(Boolean.TRUE,"保存成功");
	}
}
